package com.zx.controller.shop;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.zx.dto.request.goods.GoodsRequestDto;
import com.zx.dto.request.shop.CartRequestDto;
import com.zx.dto.response.goods.GoodsListDtoResult;
import com.zx.dto.response.shop.ShoppingCartDto;
import com.zx.model.shop.Shop;
import com.zx.model.shop.ShoppingCart;
import com.zx.model.user.User;
import com.zx.service.shop.Impl.ShoppingCartServiceImpl;
import com.zx.service.shop.ShoppingCartService;
import com.zx.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/8/3 0003 15:17
 * @mood zhou xun
 */
@Controller
@RequestMapping("/shopping")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /*查看购物车数量*/
    @GetMapping("/carts")
    @ResponseBody
    public int queryCart(@CookieValue(value = "UserToken")String userToken){
       return shoppingCartService.queryCart(userToken);
    }
    /*查询购物车集合*/
    @GetMapping("/cart/list")
    @ResponseBody
    public GoodsListDtoResult queryUserCart( HttpServletRequest request,GoodsRequestDto goodsRequestDto){
        return shoppingCartService.queryUserCart(request ,goodsRequestDto);
    }
    @GetMapping(value = "/add/cart")
    @ResponseBody
    public boolean addCart(CartRequestDto cartRequestDto, HttpServletRequest request){
        return shoppingCartService.addCart(cartRequestDto,request);
    }
    @PostMapping("/delete/cart")
    @ResponseBody
    public boolean deletdCart(List<Integer> id){
        return shoppingCartService.deleteCartGood(id);
    }
    @PostMapping(value = "/query/cart/list")
    public String queryCaryList(@RequestParam(value = "map") String map, HttpServletRequest request, Model model){
      Map<Integer,Integer> maps=(Map<Integer, Integer>)JSON.parse(map);
        GoodsRequestDto goodsRequestDto=new GoodsRequestDto();
        goodsRequestDto.setGidList(maps.keySet().stream().collect(Collectors.toList()));
        goodsRequestDto.setQuantityList(maps.values().stream().collect(Collectors.toList()));
        GoodsListDtoResult goodsListDtoResult = shoppingCartService.queryUserCart(request, goodsRequestDto);
        String json=JSON.toJSONString(goodsListDtoResult);
        model.addAttribute("goodsListDtoResult",json);
        return "getOrderInfo";
    }
}
